<?php

include_once ("Server.php");
include_once ("config.php");

//User Login

function registerUser($userName, $password)
{
	$con = connect();
	$userName = stripslashes($userName);
	$password = stripslashes($password);
	$userName = mysql_real_escape_string($userName, $con);
	$password = mysql_real_escape_string($password, $con);
	$q = "INSERT INTO user (name, password, role) ";
	$q = $q."VALUES ('".$userName."','".$password."', 'user')";
	print $q;
	query($q, $con);
	disconnect($con);
	
}

function checkLogin($userName, $password)
{
	$con = connect();
	$userName = stripslashes($userName);
	$password = stripslashes($password);
	$userName = mysql_real_escape_string($userName, $con);
	$password = mysql_real_escape_string($password, $con);

	$q="SELECT id,name,role FROM user WHERE name='".$userName."' AND password='".$password."'";
	$result= query($q, $con);
	if (mysql_num_rows($result) > 0)
	{
		$userRow =  mysql_fetch_array($result);
		$user = new User($userRow['id'],$userRow['name'],$userRow['role']);
	}
	else
	{
		$user = false;
	}
	disconnect($con);
	return $user;
}

function getUserByID($userID)
{
	$con = connect();
	$q="SELECT id,name,role FROM user WHERE id=".$userID;
	$result= query($q, $con);
	if (mysql_num_rows($result) > 0)
	{
		$userRow =  mysql_fetch_array($result);
		$user = new User($userRow['id'],$userRow['name'],$userRow['role']);
	}
	return $user;
}

function getOrphanTeams($draftID)
{
	$con = connect();
	$query = "SELECT id,name,user_id,draft_id,draft_order FROM team ";
	$query = $query . "WHERE team_id = 'null' AND draft_id=".$draftID;
	$teamRows = query($query, $con);
	if (mysql_num_rows($teamRows) > 0)
	{
		$teams = array();
		while($teamRow =  mysql_fetch_array($teamRows))
		{
			$t = new Team($teamRow['id'],
						 $teamRow['name'],
						 $teamRow['user_id'],
						 $teamRow['draft_id'],
						 $teamRow['draft_order']);
			$teams[] = $t;
		}
	}
	disconnect($con);
	return $teams;
}

function getTeamByID($teamID)
{
	$con = connect();
	$query = "SELECT id,name,user_id,draft_id,draft_order FROM team ";
	$query = $query . "WHERE id = '".$teamID;
	$teamRows = query($query, $con);
	if (mysql_num_rows($teamRows) > 0)
	{
		$teamRow =  mysql_fetch_array($teamRows);
		$team = new Team($teamRow['id'],
						 $teamRow['name'],
						 $teamRow['user_id'],
						 $teamRow['draft_id'],
						 $teamRow['draft_order']);
	}
	disconnect($con);
	return $team;
}


function findTeams($userID)
{
	$con = connect();
	$query = "SELECT id,name,user_id,draft_id,draft_order FROM team ";
	$query = $query . "WHERE user_id=".$userID;
	$teamRows = query($query, $con);
	if (mysql_num_rows($teamRows) > 0)
	{
		$teams = array();
		while($teamRow =  mysql_fetch_array($teamRows))
		{
			$t = new Team($teamRow['id'],
						 $teamRow['name'],
						 $teamRow['user_id'],
						 $teamRow['draft_id'],
						 $teamRow['draft_order']);
			$teams[] = $t
		}
	}
	disconnect($con);
	return $teams;
}

function findTeamFromDraftOrder($draft)
{
	$con = connect();
	$query = "SELECT id,name,user_id,draft_id,draft_order FROM team ";
	$query = $query . "WHERE draft_order = '".$draft->currentDraftOrder()."' AND draft_id = '".$draft->id."'";
	$teamRows = query($query, $con);
	if (mysql_num_rows($teamRows) > 0)
	{
		$teamRow =  mysql_fetch_array($teamRows);
		$team = new Team($teamRow['id'],
						 $teamRow['name'],
						 $teamRow['user_id'],
						 $teamRow['draft_id'],
						 $teamRow['draft_order']);
	}
	disconnect($con);
	return $team;
}

function getAllTeamsInDraft($draftID)
{
	$con = connect();
	$query = "SELECT id,name,user_id,draft_id,draft_order FROM team ";
	$query = $query . "WHERE draft_id =".$draftID;
	$teamRows = query($query, $con);
	if (mysql_num_rows($teamRows) > 0)
	{
		$retVal = array();
		while($teamRow =  mysql_fetch_array($teamRows))
		{
			$team = new Team($teamRow['id'],
							 $teamRow['name'],
							 $teamRow['user_id'],
							 $teamRow['draft_id'],
							 $teamRow['draft_order']);
			$retVal[] = $team;
		}
	}
	disconnect($con);
	return $retVal;
}

function addUserToTeam($teamID, $userID)
{
	$con = connect();
	$q = "UPDATE team SET user_id=".$userID." ";
	$q = $q."WHERE id=".$teamID;
	$teamRows = query($q, $con);
	if (mysql_num_rows($teamRows) > 0)
	{
		$teamRow =  mysql_fetch_array($teamRows);
		$team = new Team($teamRow['id'],
						 $teamRow['name'],
						 $teamRow['user_id'],
						 $teamRow['draft_id'],
						 $teamRow['draft_order']);
	}
	disconnect($con);
	return $team;
}

function getPlayerByID($playerID)
{
	$con = connect();
	$q = "SELECT p.id,p.first_name,p.last_name,p.nfl_team_id,p.position,p.rank,nt.location,nt.name,nt.short_name,nt.bye_week FROM player p ";
	$q = $q."JOIN nfl_team nt ON nt.id = p.nfl_team_id ";
	$q = $q."WHERE p.id=".$playerID;
	$playerRows = query($q, $con);
	if (mysql_num_rows($playerRows) > 0)
	{
		$retval = array();
		$row = mysql_fetch_array($playerRows);
		$t = new Team ($row['nfl_team_id'],
					   $row['location'],
					   $row['name'],
					   $row['short_name'],
					   $row['bye_week']);
					   
		$p = new Player($row['id'], 
						$row['first_name'], 
						$row['last_name'],
						$row['position'],
						$row['rank'],
						$t);
	}
	disconnect($con);
	return $p;
}

function getPlayersForTeam($teamID)
{
	$con = connect();
	$q = "SELECT p.id,p.first_name,p.last_name,p.nfl_team_id,p.position,p.rank,nt.location,nt.name,nt.short_name,nt.bye_week FROM player p ";
	$q = $q."JOIN selection s ON s.player_id = p.id AND s.team_id = ".$teamID." ";
	$q = $q."JOIN nfl_team nt ON nt.id = p.nfl_team_id";
	$playerRows = query($q, $con);
	if (mysql_num_rows($playerRows) > 0)
	{
		$retval = array();
		while ($row = mysql_fetch_array($playerRows))
		{
			$t = new NFLTeam($row['nfl_team_id'],
					   $row['location'],
					   $row['name'],
					   $row['short_name'],
					   $row['bye_week']);
					   
			$p = new Player($row['id'], 
						$row['first_name'], 
						$row['last_name'],
						$row['position'],
						$row['rank'],
						$t);
						
			$retVal[] = $p;
		}
	}
	disconnect($con);
	return $retVal;
}

function getRecentSelections($num, $draftID)
{
	$con = connect();
	$q = "SELECT s.id,s.team_id,s.player_id,s.round,s.pick,s.selection_time, ";
	$q = $q."p.first_name,p.last_name,p.nfl_team_id,p.position,p.rank, ";
	$q = $q."t.name,t.user_id,t.draft_id,t.draft_order FROM selection s ";
	$q = $q."JOIN player p ON s.player_id = p.id ";
	$q = $q."JOIN team t ON s.team_id = t.id ";
	$q = $q."WHERE t.draft_id=".$draftID." ";
	$q = $q."ORDER BY s.pick DESC LIMIT ".$num;
	$selectionRows = query($q, $con);
	if (mysql_num_rows($selectionRows) > 0)
	{
		$retval = array();
		while ($row = mysql_fetch_array($selectionRows))
		{
			$p = new Player($row['player_id'], 
							$row['first_name'], 
							$row['last_name'],
							$row['position'],
							$row['rank'],
							$row['nfl_team_id']);
							
			$t = new Team($row['team_id'],
						  $row['name'],
						  $row['user_id'],
						  $row['draft_id'],
						  $row['draft_order']);
						  
			$s = new Selection($t, $p, $row['id'],
							   $row['round'],
							   $row['pick'],
							   $row['selection_time']);
							   
			$retVal[] = $s;				   
		}
		
	}
	disconnect($con);
	$ret = "";
	foreach ($retVal as $sel)
	{
		$ret = $ret.$sel."<br/>";
	}
	return $ret;
	
}

//Draft Logic

function getDraft($draftID)
{
	$con = connect();
	$query = "SELECT id,name,teams,current_round,current_pick FROM draft ";
	$query = $query . "WHERE id = " . $draftID;
	$draftRows = query($query, $con);
	if (mysql_num_rows($draftRows) > 0)
	{
		$draftRow =  mysql_fetch_array($draftRows);
		$draft = new Draft($draftRow['id'], $draftRow['name'], $draftRow['teams']);
		$draft->currentRound = $draftRow['current_round'];
		$draft->currentPick = $draftRow['current_pick'];

	}
	
	disconnect($con);
	return $draft;
}

function makeSelection($playerID, $team, $draft)
{
	if ($team->isInDraft($draft->id) && $draft->isOnClock($team->draftOrder))
	{
		$con = connect();
		$query = "INSERT INTO selection (team_id, player_id, round, pick) ";
		$query = $query . "VALUES ('" . $team->id . "','" . $playerID . "','" . $draft->currentRound . "','" . $draft->currentPick . "')";
		query($query, $con);
		disconnect($con);
		updateTextFile();
		return incrementDraft($draft);
	}
	return false;
}

function updateTextFile()
{
	$fname = "mod.html"; 
	$out = time();
	$f = fopen($fname, "w");
	fwrite($f, $out); 
	fclose($f); 
}

function incrementDraft($draft)
{
	if ($draft->increment())
	{
		$con = connect();
		$q = "UPDATE draft SET current_pick='".$draft->currentPick."', current_round='".$draft->currentRound."'";
		$q= $q . "WHERE id=".$draft->id;
		query($q,$con);
		disconnect($con);
		return true;
	}
	else
	{
		finalizeDraft();
	}
}

function finalizeDraft()
{
	echo "DraFT iS dONE yO";
	return false;
}

function getAllPlayers()
{
	$con = connect();
	$q = "SELECT p.id,p.first_name,p.last_name,p.nfl_team_id,p.position,p.rank,s.id as isSelected, ";
	$q = $q."nt.location,nt.name,nt.short_name,nt.bye_week FROM player p ";
	$q = $q."JOIN nfl_team nt ON p.nfl_team_id = nt.id ";
	$q = $q."LEFT JOIN selection s ON p.id = s.player_id ";
	$q = $q."ORDER BY p.rank ASC";
	$playerRows = query($q, $con);
	if (mysql_num_rows($playerRows) > 0)
	{
		$retval = array();
		while ($row = mysql_fetch_array($playerRows))
		{
			if (!$row['isSelected'])
			{
				$t = new NFLTeam($row['nfl_team_id'], 
								 $row['location'], 
								 $row['name'],
								 $row['short_name'],
								 $row['bye_week']);
								 
				$p = new Player($row['id'], 
							$row['first_name'], 
							$row['last_name'],
							$row['position'],
							$row['rank'], $t);
				$retVal[] = $p;
			}
		}
	}
	else 
	{
		$retVal = false;
	}
  
	disconnect($con);
	return $retVal;
}


?>